# __init__.py 为初始入口文件,工程代码的入口文件.

# 导入动作库常用函数
from ascript.android.action import click,slide,Touch,gesture
# 导入控件检索相关
from ascript.android.node import Selector
# 导入图色相关
from ascript.android.screen import capture,FindColors,FindImages,Ocr
# 导入系统相关
from ascript.android import system
# 环境设备相关
from ascript.android.system import R,Device
from ascript.android.ui import Dialog
from time import time,sleep
import re
print("Hello AS!")

# Selector().id("com.smile.gifmaker:id/like_element_click_layout").desc(r"未点赞，点赞\d+").visible(True)
# Selector(1).id("com.smile.gifmaker:id/like_element_click_layout").desc(r"已点赞，点赞\d+").visible(True)
# Selector(1).id("com.smile.gifmaker:id/slide_play_avatar_click_area").desc("初中重点作者头像").visible(True)
# #Selector().id("com.smile.gifmaker:id/user_name_text_view").text("@初中重点考…﻿﻿﻿﻿﻿﻿")
# Selector().id("com.smile.gifmaker:id/user_name_text_view").text("@初中重点考….*").visible(True)
def get_nickname():
    node = Selector().id("com.smile.gifmaker:id/slide_play_avatar_click_area").desc(".*").visible(True).find()
    if node:
        return node.desc
    return None
def get_liked_nickname(temp_nickname,nickname_arr = []):
    count = 0
    nickname = None
    while True:
        count += 1
        if Selector().id("com.smile.gifmaker:id/like_element_click_layout").desc(r"已点赞，点赞\d+").visible(True).find():
            nickname = get_nickname()
        elif temp_nickname:
            nickname = temp_nickname

        if nickname:
            for record_nickname in reversed(nickname_arr):
                sub =  time() - record_nickname[1]
                if nickname == record_nickname[0]:
                    if sub > 5:
                        print(f"sub > 5秒 = {sub}")
                        nickname_arr.remove(record_nickname)
                        break
            nickname_arr.append([nickname,time()])
            nickname_arr = nickname_arr[-3:]
            print(f"新添加 nickname_arr = {nickname_arr}")
            break
        
        if count >= 5:
            break
        sleep(0.5)
    for i in range(5):
        if Selector().id("com.smile.gifmaker:id/like_element_click_layout").desc(r"未点赞，点赞\d+").visible(True).find() or \
            len(nickname_arr) > 0 and get_nickname() == nickname_arr[-1]:
            break
        sleep(0.3)

    return nickname_arr
    

def compare_nickname(nickname_arr,count_zan,self_zan):
    old_count_zan = count_zan
    old_self_zan = self_zan

    nickname = get_nickname()
    if not nickname:
        return nickname_arr,count_zan,self_zan
        #print(f"nickname = {nickname}")
    node = Selector().id("com.smile.gifmaker:id/like_element_click_layout").desc(r"[已未]点赞，点赞\d+").visible(True).find()
    if node:
        #Dialog.toast(f"nickname = {nickname}",dur=1000)
        for record_nickname in reversed(nickname_arr):
            if nickname == record_nickname[0]:
                if re.findall("未点赞",node.desc):
                    count_zan += 1
                    click(node.rect.centerX(),node.rect.centerY())
                    Dialog.toast("已点击",dur=1000)
                    print(f"已点击 {nickname}")
                    sleep(0.5)
                elif re.findall("已点赞",node.desc):
                    Dialog.toast("该视频已是点赞成功状态",dur=1000)
                    print("该视频已是点赞成功状态")
                    self_zan += 1 

                if Selector().id("com.smile.gifmaker:id/like_element_click_layout").desc(r"已点赞，点赞\d+").visible(True).find() or \
                    get_nickname() != nickname and (count_zan > old_count_zan or self_zan > old_self_zan):
                    nickname_arr.remove(record_nickname)
                    print(f"remove {record_nickname},left_arr = {nickname_arr}")
                    return nickname_arr,count_zan,self_zan

    return nickname_arr,count_zan,self_zan
    
def 快手无响应():
    if Selector().text("快手没有响应").find():
        if Selector().text("确定").type("Button").find().click():
            sleep(2)
def 打开推送通知():
    if Selector().text("打开推送通知").find():
        if Selector().id("com.smile.gifmaker:id/negative").text("^忽略$").find().click():
            sleep(0.5)
nickname_arr = []
count = 0
count_zan = 0
self_zan = 0
while True:
    count += 1
    print(f"第{count}次循环,我方点赞 = {count_zan},对方点赞 = {self_zan},合计点赞 = {count_zan+self_zan}，正在检测点赞，请稍后...")
    temp_nickname = None
    ks_crash = 0
    while True:
        if Selector().id("com.smile.gifmaker:id/like_element_click_layout").desc(r"已点赞，点赞\d+").visible(True).find():
            print(f"temp_nickname = {temp_nickname}")
            break 
        else:
            temp_nickname = get_nickname()
        if ks_crash > 5:
            快手无响应()
            ks_crash = 0
        ks_crash += 1
        sleep(0.3)
    for record_nickname in reversed(nickname_arr):
        sub =  time() - record_nickname[1]
        if sub > 15:
            nickname_arr.remove(record_nickname)
    nickname_arr = get_liked_nickname(temp_nickname,nickname_arr = nickname_arr)
    打开推送通知()
    if len(nickname_arr) >= 1:
        old_zan = count_zan + self_zan
        for i in range(10):
            if count_zan + self_zan > old_zan:
                print("sleep 15 秒")
                sleep(15)
                break
            print(f"第 i = {i} 次准备点赞 ")
            nickname_arr,count_zan,self_zan = compare_nickname(nickname_arr,count_zan,self_zan)
            sleep(0.5)

    
    
    
    




